// Help
// 
// 保佑没Bug 
//
// Created by AS on 2019/4/8. 
//
// Copyright © 2018 AS. All rights reserved.
// 

#import "TabPageViewController.h"
#import "TYTabPagerBar.h"
#import "TYPagerController.h"
#import "MTypeModel.h"
#import "OpenNumberViewController.h"
@interface TabPageViewController ()<TYTabPagerBarDataSource,TYTabPagerBarDelegate,TYPagerControllerDataSource,TYPagerControllerDelegate>

@property (nonatomic, weak) TYTabPagerBar *tabBar;
@property (nonatomic, weak) TYPagerController *pagerController;
/**  */
@property (nonatomic,strong) NSMutableArray *dataSource;
@end

@implementation TabPageViewController

-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = HexRGB(0xf3f5f5);
    self.navigationItem.title = @"彩票开奖";
    [self addTabPageBar];
    [self addPagerController];
    [self getAllLottery];
}



-(void)getAllLottery{
    
    
    NSString *url = @"/api/lotteryCategory/getParentLotteryCategory";
    [HTTPTools POST:url parameters:[NSMutableDictionary new] callback:^(id result, NSString *msg, NSError *error) {
       
        if (!error) {
            if (!msg) {
                NSArray *array = [MTypeModel mj_objectArrayWithKeyValuesArray:result];
                
                MTypeModel *model = [MTypeModel new];
                model.name = @"全部彩种";
                model.code = @"";
                [self.dataSource addObject:model];
                [self.dataSource addObjectsFromArray:array];
                [self.tabBar reloadData];
                [self.pagerController reloadData];
            }
        }
    }];
}


- (void)addTabPageBar {
    TYTabPagerBar *tabBar = [[TYTabPagerBar alloc]init];
    tabBar.frame = CGRectMake(0,0, CGRectGetWidth(self.view.frame), 36);
    tabBar.layout.barStyle = TYPagerBarStyleProgressBounceView;
    tabBar.layout.normalTextColor = HexRGB(0x888888);
    tabBar.layout.selectedTextColor = mMainColor;
    tabBar.layout.progressColor =  mMainColor;
    tabBar.dataSource = self;
    tabBar.delegate = self;
    tabBar.backgroundColor = [UIColor whiteColor];
    [tabBar registerClass:[TYTabPagerBarCell class] forCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier]];
    [self.view addSubview:tabBar];
    self.tabBar = tabBar;
}

- (void)addPagerController {
    TYPagerController *pagerController = [[TYPagerController alloc]init];
    pagerController.layout.prefetchItemCount = 1;
    pagerController.layout.addVisibleItemOnlyWhenScrollAnimatedEnd = YES;
    pagerController.dataSource = self;
    pagerController.delegate = self;
    pagerController.view.frame = CGRectMake(0, self.tabBar.maxY,ScreenWidth,ScreenHeight - self.tabBar.maxY);
    pagerController.view.backgroundColor = ClearColor;
    [self addChildViewController:pagerController];
    [self.view addSubview:pagerController.view];
    _pagerController = pagerController;
}

- (NSInteger)numberOfItemsInPagerTabBar {
    return self.dataSource.count;
}

- (UICollectionViewCell<TYTabPagerBarCellProtocol> *)pagerTabBar:(TYTabPagerBar *)pagerTabBar cellForItemAtIndex:(NSInteger)index {
    UICollectionViewCell<TYTabPagerBarCellProtocol> *cell = [pagerTabBar dequeueReusableCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier] forIndex:index];
    MTypeModel *model = self.dataSource[index];
    cell.titleLabel.text = model.name;
    return cell;
}

#pragma mark - TYTabPagerBarDelegate
- (NSInteger)numberOfControllersInPagerController {
    return self.dataSource.count;
}
- (CGFloat)pagerTabBar:(TYTabPagerBar *)pagerTabBar widthForItemAtIndex:(NSInteger)index {
    MTypeModel *model = self.dataSource[index];
    return [pagerTabBar cellWidthForTitle:model.name];
}

- (void)pagerTabBar:(TYTabPagerBar *)pagerTabBar didSelectItemAtIndex:(NSInteger)index {
    [_pagerController scrollToControllerAtIndex:index animate:YES];
}
- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index prefetching:(BOOL)prefetching {
    MTypeModel *model = self.dataSource[index];
    OpenNumberViewController *view = [OpenNumberViewController new];
    view.code = model.code;
    return view;
}

#pragma mark - TYPagerControllerDelegate

- (void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex animated:(BOOL)animated {
    [_tabBar scrollToItemFromIndex:fromIndex toIndex:toIndex animate:animated];
}

-(void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex progress:(CGFloat)progress {
    [_tabBar scrollToItemFromIndex:fromIndex toIndex:toIndex progress:progress];
}


@end
